package com.ldg.search.listener;

import com.alibaba.fastjson.JSON;
import com.ldg.search.entity.ItemEntity;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-09-27-21:51
 * @Description:
 */
@Component
@RabbitListener(queues = "updateStatus")
public class MyRmqGoodsListener {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @RabbitHandler
    public void updateStatus(String itemEntities){
        //将得到的sku列表转换为对象
        List<ItemEntity> entities = JSON.parseArray(itemEntities, ItemEntity.class);
        //更新到索引库中
        restTemplate.save(entities);
        //提示
        System.out.println("更新到索引库成功！");
    }
}
